package Final;

import com.sun.deploy.uitoolkit.ToolkitStore;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Delete {
    static JLabel labID = new JLabel();
    static JTextField textID = new JTextField();

    public Delete() {
        JFrame jf = new JFrame();
        jf.setTitle("删除界面");
        jf.setSize(340, 200);
        //jf.setDefaultCloseOperation(3);
        jf.setLayout(new FlowLayout());
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);

        java.awt.FlowLayout fl = new java.awt.FlowLayout(FlowLayout.CENTER, 5, 5);
        jf.setLayout(fl);

        java.awt.Dimension dim2 = new java.awt.Dimension(50, 20);//标签的大小
        java.awt.Dimension dim3 = new java.awt.Dimension(250, 30);//输入框的大小
        java.awt.Dimension dim4 = new java.awt.Dimension(70, 40);//按钮的大小


        //添加说明标签
        JLabel jLabel = new JLabel("请输入");
        jLabel.setPreferredSize(dim2);
        jf.add(jLabel);

        //添加“学号”标签

        labID.setText("学号");
        labID.setPreferredSize(dim2);
        jf.add(labID);

        //添加账号输入框，并添加监控事件

        textID.setPreferredSize(dim3);
        jf.add(textID);

        //添加一个删除按钮
        JButton button_delete = new JButton();
        button_delete.setText("删除");
        button_delete.setPreferredSize(dim4);
        button_delete.setBounds(10, 300, 300, 40);
        jf.add(button_delete);

        //添加取消按钮
        JButton button_cancle = new JButton();
        button_cancle.setText("取消");
        button_cancle.setPreferredSize(dim4);
        button_cancle.setBounds(10, 300, 300, 40);
        jf.add(button_cancle);


        jf.setVisible(true);

        button_delete.addActionListener(e -> {
            Connection conn = Sql.open();

            String sql = "delete from Student where ID=?";


            PreparedStatement pstmt = null;
            try {
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, textID.getText());
                int result;
                result = pstmt.executeUpdate();
                if(result>0 ){
                    new Succeed();
                }else {
                    new False();
                }


            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                Sql.close(conn);
            }


        });
        button_cancle.addActionListener(g->{jf.dispose();});
        }
        static class Succeed{
        public Succeed(){
            JFrame jFrame = new JFrame();
            jFrame.setTitle("成功界面");
            jFrame.setSize(340,100);
            jFrame.setDefaultCloseOperation(3);
            jFrame.setLayout(new FlowLayout());
            jFrame.setLocationRelativeTo(null);
            jFrame.setResizable(false);

            JLabel jLabel1 = new JLabel("删除成功！");
            JButton jButton = new JButton("返回");
            jButton.addActionListener(g -> {

                jFrame.dispose();

            });

            jFrame.add(jLabel1);
            jFrame.add(jButton);
            jFrame.setVisible(true);

        }


        }
        static class False{
            public False(){
                JFrame jFrame = new JFrame();
                jFrame.setTitle("提示界面");
                jFrame.setSize(340, 100);
                jFrame.setLayout(new FlowLayout());
                jFrame.setLocationRelativeTo(null);
                jFrame.setResizable(false);

                JLabel jLabel = new JLabel();
                jLabel.setText("无此id！！");

                JButton jButton = new JButton();
                jButton.setText("返回");

                jFrame.add(jLabel);
                jFrame.add(jButton);
                jFrame.setVisible(true);

                jButton.addActionListener(e -> {

                    jFrame.dispose();

                });
            }
        }
}
